#!/usr/bin/python

import MySQLdb
import htmlUtils
import cgi;

# this gets around a nasty bug which
# assumes os.environ['USER'] will be set
import os
os.environ['USER'] = "apache"
import db_info

db = MySQLdb.connect(host=db_info.DB_HOST, user=db_info.DB_USERNAME, passwd=db_info.DB_PASSWORD, db=db_info.DB_DATABASE)

print "Content-Type: text/html\n\n";

print """
<html>
<head>
  <title>SemanticSQL Dictionary</title>
  <link rel="StyleSheet" href="style.css">

  <script>
    function focusText() {
      var tbox = document.getElementById("input_textbox");
      tbox.focus();
    }
  </script>
</head>

<body onLoad="focusText()">
<font size=6><b>SemanticSQL Dictionary/Lexicon</b></font><br>
<font size=4><br>
<a href="index.py">Back to Parser</a><br>
<p>
"""

def table(table_name):
    htmlUtils.writeTable(db, table_name);

    print '''
    <form action="dictionary.py" method="GET">
      <input type="hidden" name="table_name" value="{}">
      <input type="textbox" size=60 name="value">
      <input type="submit" value="Add">
    </form>
    <p>
    '''.format(table_name)

    print "<hr>"

import init_db
import generate_grammar_from_db
form = cgi.FieldStorage()
if (form.has_key("table_name") and form.has_key("value")):
    table_name = form["table_name"].value;
    value = form["value"].value;
    cmd = init_db.insert_cmd(table_name, value.split())
    db.cursor().execute(cmd)
    db.commit()
    generate_grammar_from_db.main(open('grammar.fcfg', 'w'), db)

dicts = ['PropN', 'regular-N', 'irregular-N', 'regular-IV', 'regular-TV', 'ADJ']
for d in dicts:
    table(d)
    
print '</body>';

print '</html>';
print "\n";


